package main

import (
	"fmt"
	"log"
	"os"
	"strings"
	"time"

	"github.com/androidsr/sc-go/sgorm"
	"github.com/androidsr/sc-go/sno"
	"github.com/androidsr/sc-go/syaml"
)

func init() {
	log.SetFlags(log.Llongfile | log.LstdFlags)
	config, err := syaml.LoadFile[syaml.ScRoot]("sc-go.yaml")
	if err != nil {
		panic(err)
	}
	sgorm.DB = sgorm.New(config.Sc.Gorm)
	sno.New(*config.Sc.Snowflake)
}

func main() {
	if len(os.Args) != 4 {
		fmt.Println("menu_scheme_id=查询条件", "menu_scheme_id=新租户值", "删除新租户相关菜单：true|false")
		return
	}
	data := make([]SysMenu, 0)
	query := SysMenu{
		MenuSchemeId: os.Args[1],
	}
	err := sgorm.DB.SelectList(&data, query)
	if err != nil {
		fmt.Println(err)
		return
	}
	deleteFlag := os.Args[3]
	if strings.ToLower(deleteFlag) == "true" {
		sgorm.DB.Delete(&SysMenu{MenuSchemeId: os.Args[2]}, "menu_scheme_id = ?", os.Args[2])
	}
	first := time.Now().Format("060102150405")
	for _, v := range data {
		v.Id = first + v.Id
		if v.Pid != "" && v.Pid != "root" {
			v.Pid = first + v.Pid
		}
		v.MenuSchemeId = os.Args[2]
		v.CreateTime = time.Now().Format("20060102150405")
		sgorm.DB.Save(v)
	}
}

type SysMenu struct {
	Id              string `gorm:"primaryKey"`
	MenuSchemeId    string
	ApplicationId   string
	ShowName        string
	CallUrl         string
	MenuType        int
	Icons           string `gorm:"column:icons"`
	Pid             string
	Deleted         int
	State_          int `gorm:"column:state_"`
	CreateUser      string
	CreateTime      string
	Remark_         string `gorm:"column:remark_"`
	Sort_           int    `gorm:"column:sort_"`
	Urlopenmode     string
	MenuCode        string
	Quickicons      string
	Sysurl          string
	Remindurl       string
	RetrialEnabled  string
	MenuTags        string
	Attr            string
	AppId           string
	ThreeRightsType int
	State           int
	SortNum         int
	CreateUserId    string
	Remark          string
	BusinessStatus  int
	BusinessWarnMsg string
}
